package service;

import mapper.BookMapper;
import org.springframework.stereotype.Service;
import pojo.Book;
import tools.Page;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/1/4.
 */
@Service("bookService")
public class BookServiceImpl implements BookService {
    @Resource
    BookMapper bookMapper;

    public Page getBookList(Integer id, String name, Page pageObj){
        // 查询总条数
        int totalCount = bookMapper.getCount(id,name);
        // 设置总数、总页数
        pageObj.setTotalCount(totalCount);
        // 如果总条数>0,说明数据库中有数据，再查询具体数据
        if (totalCount > 0) {
            // （用户一直点击下一页）如果当前页大于总页数，把总页数的值赋给当前页的值，显示最后一页的值
            if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                pageObj.setCurrPageNo(pageObj.getTotalPageCount());
            List<Book> lrsList = bookMapper.getBookList(id,name,(pageObj.getCurrPageNo()-1)*pageObj.getPageSize(), pageObj.getPageSize());
            System.out.println(lrsList.size()
            );
            pageObj.setList(lrsList);
        } else {
            pageObj.setCurrPageNo(0);
            pageObj.setList(new ArrayList<Book>());
        }
        return pageObj;

  }

    @Override
    public int add(Book book) {
        return bookMapper.add(book);
    }

    @Override
    public int gitUserByid(Book book) {
        return bookMapper.gitUserByid(book);
    }

    @Override
    public Object uderCodeIsExit(String userCode) {
        return null;
    }

}
